<template>
  <div class="app-container">
    <div class="filter-container">
      名称:
      <el-input v-model="listQuery.name" label="名称" placeholder="关键词" style="width: 200px;" class="filter-item mring10"
        @keyup.enter.native="handleFilter" suffix-icon="el-icon-search" />
      <el-button v-waves class="filter-item mring10" type="primary" icon="el-icon-search" @click="handleFilter"
        size="small"> 搜索</el-button>
      <el-button v-waves type="success" icon="el-icon-plus" @click="handleCreate()" size="small"> 添加规格</el-button>
    </div>

    <el-table :key="tableKey" v-loading="listLoading" :data="list" fit stripe highlight-current-row
      style="width: 100%;margin-top: 1.125rem" row-key="id">
      <el-table-column label="规格名" prop="title" min-width="150"></el-table-column>
      <el-table-column label="规格值" prop="spec_name" min-width="150"></el-table-column>
      <el-table-column label="类型" prop="show_type" min-width="150">
        <template slot-scope="{row,$index}">
          {{ textType[row.show_type] }}
        </template>
      </el-table-column>
      <el-table-column label="排序" prop="sort" min-width="120"></el-table-column>
      <el-table-column label="创建时间" prop="create_time" min-width="150" align="center">
        <template slot-scope="{row,$index}">
          {{ row.create_time | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" @click="handleUpdate(row)" size="small">编 辑</el-button>
          <el-button type="danger" @click="handleDelete(row, $index)" size="small">删 除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination hide-on-single-page v-show="total > 0" :total="total" :page.sync="listQuery.page"
      :limit.sync="listQuery.size" @pagination="getList" />
  </div>
</template>
<script>
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination'
import { fetchList, deleteBaseSpec } from '@/api/goods/baseSpec'

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      list: [],
      tableKey: '',
      listLoading: false,
      total: 0,
      listQuery: {
        size: 10,
        page: 1
      },
      textMap: {
        update: '编辑分类',
        create: '新增分类'
      },
      textType: {
        1: '文字',
        2: '颜色',
        3: '图片',
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.data
        //this.list.map(v=>{ v.status_name = v.status == 1 ? '启用' : '禁用'; })
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push({ name: 'createBaseSpec' })
    },
    handleUpdate(row) {
      this.$router.push({ name: 'createBaseSpec', params: { id: row.id } })
    },
    handleDelete(row, index) {
      deleteBaseSpec(row).then(response => {
        if (response.code == 200) {
          this.$notify({
            title: 'Success',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })

          this.list.splice(index, 1)
        }
      })
    }
  }
}

</script>
<style type="text/css">
.avatar {
  max-width: 7.5rem;
  height: auto;
  max-height: 7.5rem;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 90px;
  height: 90px;
  line-height: 90px;
  text-align: center;
}

.filter-container {
  background: #FFFFFF;
  padding: 0.625rem 1.125rem;
}

table td {
  font-size: 12px;
  color: #8c8c8c;
}

.filter-container {
  font-size: 12px;
}

.filter-item input[type='text'] {
  height: 32px;
  line-height: 32px;
  font-size: 12px;
}

.mring10 {
  margin-right: 10px;
}
</style>
